package component.security.servlet;


import java.text.SimpleDateFormat;
import javax.servlet.ServletException;

import component.security.util.EncryptSecurityManager;



/**
 * 启动安全Servlet
 *
 */
public class InitServlet extends javax.servlet.http.HttpServlet {	

	/**
	 * 在这个方法里对系统做一些初始化工作
	 * @preserve
	 */
	public void init(javax.servlet.ServletConfig config)throws ServletException {
		super.init(config);	
		check();
	}
	
	public void check() throws ServletException {		
	
		//检验用户:是否有效、是否超出试用期、网卡是否正确
		EncryptSecurityManager manager = EncryptSecurityManager.getInstance();
		
		//证书判断
		if (!manager.checkUserValidate()){
			System.out.print("对不起，您使用了无效的证书！");			
			System.exit(-1);		
		}
		
		//有效期判断
		String expiryDate = (String)manager.getValue("ExpiryDate");		
		if (expiryDate!=null) {
			SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
			String currentDate = tempDate.format(new java.util.Date());
			if (currentDate.compareTo(expiryDate) >0) {
				System.out.print("对不起，超出试用期，请使用正版软件，谢谢！");
				System.exit(-1);
			}
		}
		
		//判断网卡
		String networdCard = (String)manager.getValue("NetworkCard");
		if (networdCard!=null){
			component.security.hardware.NetWorkCardInfo netcard = new component.security.hardware.JavaNetWorkCardInfo();
			String currentNet = netcard.getMacInfo();
			if (!networdCard.equals(currentNet)){
				System.out.print("对不起，只能在您申请的电脑上安装该软件，谢谢！");
				System.exit(-1);	
			}
		}
	}
	/**
	 * @preserve 
	 */	 
	public void destroy() {
		
	}
}
